<?php
/**
 * @特惠赚、商品控制器API
 * Author: 小谢
 * Date: 2019-5-21
 */
namespace app\api\controller;
use app\api\logic\PageLogic;

class Goods extends Base
{
  
    /**
     *  获取对应商品详细信息
     */
    public function detail() {
        $goods_id = I('get.goods_id');
        if(!is_numeric($goods_id)) {
            $this->ajaxReturn($this->callBack(0,0,'goods_id参数错误',self::emptyObject()),'JSON');
        }
        $goods = M('goods')->field('goods_id,title,pic_url,small_images,price,desc')->where(['goods_id'=>$goods_id])->find();
        if($goods) {
            $goods['small_images'] = unserialize($goods['small_images']);
            $this->ajaxReturn($this->callBack(1,0,'成功',$goods),'JSON');
        }
        $this->ajaxReturn($this->callBack(0,0,'暂无商品详情信息',self::emptyObject()),'JSON');
    }

    /**
     * 商品评论列表
     */
    public function comment() {
        $goods_id = I('get.goods_id');
        if(!is_numeric($goods_id)) {
            $this->ajaxReturn($this->callBack(0,0,'goods_id参数错误',self::emptyObject()),'JSON');
        }
        $condition = ['parent_id'=>0,'goods_id'=>$goods_id];
        //分页参数
        $page = I('get.page') ? intval(I('get.page')) : 1 ;
        $pageSize = 20;
        $count = M('comment')->where($condition)->count();
        $PageLogic = new PageLogic($page,$pageSize,$count);
        $pageInfo = $PageLogic->pageHandle();//分页操作
        $commentList = M('comment')->alias('c')->join('__USER__ u','c.user_id =u.user_id')->where(['goods_id'=>$goods_id])->field('c.comment_id,c.goods_id,c.username,c.content,c.add_time,c.img,u.head_ico')->order('c.add_time DESC')->limit($pageInfo['pageStart'],$pageInfo['pageSize'])->select();
        if($commentList) {
            $this->ajaxReturn($this->callBack(1,0,'成功',['page'=>$page,'pageSize'=>$pageSize,'totalPage'=>$pageInfo['totalPage'],'list'=>$commentList]),'JSON');
        }
        $this->ajaxReturn($this->callBack(0,0,'暂无评论数据',self::emptyObject()),'JSON');
    }


}
